package com.wrench.elevator.tida.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.domain.Page;
import com.wrench.elevator.tida.domain.wuye.Wuye_Power;
import com.wrench.elevator.tida.domain.wuye.Wuye_Role;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.service.JSGLService;
import com.wrench.elevator.tida.util.WrenchUtil;

@Controller
@RequestMapping("/role_manage")
public class RoleManageController
{
    private static final Logger logger = LoggerFactory.getLogger(RoleManageController.class);
    
    @Resource
    private JSGLService jSGLService;
    
    @RequestMapping(value = "/getWuyeRoleByRoleId", method = RequestMethod.GET)
    public void getWuyeRoleByRoleId(HttpServletRequest request, HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in getWuyeRoleByRoleId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String role_id=WrenchUtil.getPara(request, "role_id");
        Wuye_Role role=null;
        WrenchException error=null;
        try {
			role=jSGLService.getWuyeRoleByRoleId(role_id);
		} catch (WrenchException e) {
			// TODO Auto-generated catch block
			error=e;
		}
        
        if (WrenchUtil.hasNull(error))
        {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "getWuyeRoleByRoleId  success");
            map.put("role", role);
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        
        
        logger.debug("==>**********exit out getWuyeRoleByRoleId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }
    
    
    @RequestMapping(value = "/getWuyePower", method = RequestMethod.GET)
    public void getWuyePower(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in getWuyePower(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        List<Wuye_Power> powers = null;
        WrenchException error = null;
        try
        {
            powers = jSGLService.getWuyePower();
        }
        catch (WrenchException e)
        {
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "getWuyePower success");
            map.put("powers", powers);
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        
        logger.debug("==>**********exit out getWuyePower(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        
    }
    
    @RequestMapping(value = "/addRole", method = RequestMethod.POST)
    public void addRole(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in addRole(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String role_name = WrenchUtil.getPara(request, "role_name");
        String[] power_id = WrenchUtil.getParas(request, "power_id");
        logger.debug("==> params : role_name = {} , powerid = {} ", role_name, power_id);
        WrenchException error = null;
        try
        {
            jSGLService.addRole(role_name, power_id, map);
        }
        catch (WrenchException e)
        {
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            Object flag = map.get("flag");
            map.clear();
            if ("SUCCESS".equals(flag))
            {
                map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
                map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
                map.put("message", "addRole success");
            }
            else if ("EXIST".equals(flag))
            {
                map.put(Result.RESULT_CODE, Result.ROLE_IS_EXIST_CODE);
                map.put(Result.RESULT_MESSAGE, Result.ROLE_IS_EXIST_MESSAGE);
            }
            else
            {
                map.put(Result.RESULT_CODE, Result.OTHER_ERROR_CODE);
                map.put(Result.RESULT_MESSAGE, Result.OTHER_ERROR_MESSAGE);
            }
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        
        logger.debug("==>**********exit out addRole(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
    }
    
    @RequestMapping(value = "/queryWuyeRoles", method = RequestMethod.GET)
    public void queryWuyeRoles(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in queryWuyeRoles(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String current_page = WrenchUtil.getPara(request, "current_page");
        String limit = WrenchUtil.getPara(request, "limit");
        logger.debug("==> params : current_page = {} , limit = {} ", current_page, limit);
        
        Page page = null;
        WrenchException error = null;
        
        try
        {
            page = jSGLService.queryWuyeRoles(current_page, limit);
        }
        catch (WrenchException e)
        {
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryWuyeRoles success");
            map.put("page", page);
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        
        logger.debug("==>**********exit out queryWuyeRoles(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        
    }
    
    @RequestMapping(value = "/delRole", method = RequestMethod.POST)
    public void delRole(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in delRole(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String role_id = WrenchUtil.getPara(request, "role_id");
        logger.debug("==> params : role_id = {}", role_id);
        WrenchException error = null;
        try
        {
            jSGLService.delRole(role_id, map);
        }
        catch (WrenchException e)
        {
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            Object flag = map.get("flag");
            map.clear();
            if ("SUCCESS".equals(flag))
            {
                map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
                map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
                map.put("message", "delRole success");
            }
            
            else
            {
                map.put(Result.RESULT_CODE, Result.OTHER_ERROR_CODE);
                map.put(Result.RESULT_MESSAGE, Result.OTHER_ERROR_MESSAGE);
            }
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        
        logger.debug("==>**********exit out delRole(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        
    }
    
    @RequestMapping(value = "/updateRole", method = RequestMethod.POST)
    public void updateRole(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in updateRole(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String role_id = WrenchUtil.getPara(request, "role_id");
        String role_name = WrenchUtil.getPara(request, "role_name");
        String[] power_id = WrenchUtil.getParas(request, "power_id");
        logger.debug("==> params : role_id = {} , role_name = {} , power_id = {}", role_id, role_name, power_id);
        WrenchException error = null;
        try
        {
            jSGLService.updateRole(role_id, role_name, power_id, map);
        }
        catch (WrenchException e)
        {
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            Object flag = map.get("flag");
            map.clear();
            if ("SUCCESS".equals(flag))
            {
                map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
                map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
                map.put("message", "updateRole success");
            }
            
            else
            {
                map.put(Result.RESULT_CODE, Result.OTHER_ERROR_CODE);
                map.put(Result.RESULT_MESSAGE, Result.OTHER_ERROR_MESSAGE);
            }
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        
        logger.debug("==>**********exit out updateRole(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        
    }
    
}
